﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Xml.Linq;
using Telerik.Web.UI;
using System.IO;

namespace Reconciliation.Web.Users_Control
{
    public partial class CaseViewDetails : System.Web.UI.UserControl
    {
        public int orderId;
        public int OrderID
        {
            get
            {
                return orderId;
            }
            set
            {
                orderId = value;
            }
        }

        public Boolean showALink;
        public Boolean LinkShowAComment
        {
            get
            {
                return showALink;
            }
            set
            {
                showALink = value;
            }
        }

        public Boolean showCLink;
        public Boolean LinkShowCComment
        {
            get
            {
                return showCLink;
            }
            set
            {
                showCLink = value;
            }
        }

        protected Services.CaseHeader caseHeader = new Services.CaseHeader();
        protected void Page_Load(object sender, EventArgs e)
        {
            
            string s = Request.QueryString["OrderID"];
            OrderID = int.Parse(s);
            lblOrderID.Text = "CIMB " + Resources.Lang.Order_ID + " " + s;

            caseHeader = Services.Services.GetCaseHeader(OrderID);

            if (!Context.User.IsInRole("All"))
            {
                pnlCaseStatus.Visible = false;
                pnlCaseStatus2.Visible = true;
                imgStatus.Src = "../images/icons/Case_" + caseHeader.CaseStatus + ".png";
                lblStatus.Text = caseHeader.CaseStatus;
            }
            else
            {
                pnlCaseStatus.Visible = true;
                pnlCaseStatus2.Visible = false;
            }
            
            lblDeliveryDate.Text = BasePage.FormatDate(caseHeader.DeliveryDate);
            lblCollectionDate.Text = BasePage.FormatDate(caseHeader.CollectionDate);

            lnlPersonnel.Attributes.Add("onClick", "PersonnelView('" + OrderID + "'); return false;");

            if (!IsPostBack)
            {
                if (Context.User.IsInRole("All"))
                {                    
                    foreach (ListItem lstItem in RadioButtonListStatus.Items)
                    {
                        if (lstItem.Value == caseHeader.CaseStatus)
                        {
                            lstItem.Selected = true;
                            break;
                        }
                    }
                }

                LinkShowAComment = Context.User.IsInRole("All");
                LinkShowCComment = Context.User.IsInRole("All");
                lnkAttachment.Visible = Context.User.IsInRole("All");
                BindGrid();
                BindAttachment();
            }
        }

        //override protected void OnInit(EventArgs e)
        //{
        //    //
        //    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
        //    //
        //    InitializeComponent();
        //    base.OnInit(e);
        //}

        private void BindGrid()
        {
            List<Services.CaseDetail> caseDetailList = new List<Services.CaseDetail>();
            caseDetailList = Services.Services.GetCaseDetail(OrderID);

            grid1.DataSource = caseDetailList;
            grid1.DataBind();

            grid2.Columns[0].HeaderText = "<a style=\"color:#000\" href=\"javascript:viewExpectedReturns(" + OrderID + ")\">" + Resources.Lang.Expected_Return + "</a>";
            grid2.DataSource = caseDetailList;
            grid2.DataBind();
        }

        protected void RadListViewClientComment_NeedDataSource(object sender, RadListViewNeedDataSourceEventArgs e)
        {
            RadListViewClientComment.DataSource = Services.Services.GetCaseClientComment(caseHeader.CaseID);             
        }

        protected void RadListViewAuditor_NeedDataSource(object sender, RadListViewNeedDataSourceEventArgs e)
        {
            RadListViewAuditor.DataSource = Services.Services.GetCaseAuditorComment(caseHeader.CaseID);            
        }

        protected void btnAddClientComment_Click(object sender, EventArgs e)
        {
            RadListViewClientComment.ShowInsertItem(RadListViewInsertItemPosition.LastItem);
            (sender as LinkButton).Visible = false;
            LinkShowCComment = false;
            
        }

        protected void btnAddAuditorComment_Click(object sender, EventArgs e)
        {
            RadListViewAuditor.ShowInsertItem(RadListViewInsertItemPosition.LastItem);
            (sender as LinkButton).Visible = false;
            LinkShowAComment = false;
           
        }

        protected void RadListViewAuditor_ItemCommand(object sender, RadListViewCommandEventArgs e)
        {
            if (e.CommandName == RadListView.CancelCommandName || e.CommandName == RadListView.PerformInsertCommandName)
            {
                RadListViewEditableItem insertedItem = (RadListViewEditableItem)e.ListViewItem;

                if (e.CommandName == RadListView.PerformInsertCommandName)
                {
                    Services.CaseComment caseComment = new Services.CaseComment();
                    caseComment.CaseID = caseHeader.CaseID;
                    caseComment.ClientName = ((TextBox)insertedItem.FindControl("txtCommenTitle")).Text;
                    caseComment.Comment = ((TextBox)insertedItem.FindControl("txtCommentText")).Text;
                    caseComment.UserID = int.Parse(Session["UserID"].ToString());
                    Services.Services.SetCaseAuditorComment(caseComment);
                }

                RadListViewAuditor.InsertItemPosition = RadListViewInsertItemPosition.None;
                RadListViewAuditor.FindControl("btnAddAuditorComment").Visible = true;
                LinkShowAComment = true;
            }
        }

        protected void RadListViewClientComment_ItemCommand(object sender, RadListViewCommandEventArgs e)
        {
            if (e.CommandName == RadListView.CancelCommandName || e.CommandName == RadListView.PerformInsertCommandName)
            {
                RadListViewEditableItem insertedItem = (RadListViewEditableItem)e.ListViewItem;
               
                if (e.CommandName == RadListView.PerformInsertCommandName)
                {
                    Services.CaseComment caseComment = new Services.CaseComment();
                    caseComment.CaseID = caseHeader.CaseID;                    
                    caseComment.ClientName = ((TextBox)insertedItem.FindControl("txtCommenTitle")).Text;
                    caseComment.Comment = ((TextBox)insertedItem.FindControl("txtCommentText")).Text;
                    caseComment.UserID = int.Parse(Session["UserID"].ToString());
                    Services.Services.SetCaseClientComment(caseComment);
                }

                RadListViewClientComment.InsertItemPosition = RadListViewInsertItemPosition.None;
                RadListViewClientComment.FindControl("btnAddClientComment").Visible = true;
                LinkShowCComment = true;
            }
        }

        private void BindAttachment()
        {
            repeaterResults.DataSource = Services.Services.GetCaseAttachment(caseHeader.CaseID);
            repeaterResults.DataBind();

            if (repeaterResults.Items.Count > 0)
            {
                repeaterResults.Visible = true;
                labelNoResults.Visible = false;
            }
            else
            {
                labelNoResults.Visible = true;
                repeaterResults.Visible = false;
            }
        }

        protected void buttonUploadAttch_Click(object sender, System.EventArgs e)
        {
            if (RadUpload1.UploadedFiles.Count > 0)
            {   
                foreach (UploadedFile UplFile in RadUpload1.UploadedFiles)
                {
                    Services.CaseAttachment caseAttach = new Services.CaseAttachment();
                    caseAttach.AttachmentFullName = UplFile.FileName;
                    byte[] buffer = new byte[UplFile.InputStream.Length];
                    long position = 0;
                    int currentByte = UplFile.InputStream.ReadByte();
                    while (currentByte != -1)
                    {
                        buffer[position++] = (byte)currentByte;
                        currentByte = UplFile.InputStream.ReadByte();
                    }
                    caseAttach.Attachment = buffer;
                    caseAttach.CaseID = caseHeader.CaseID;
                    Services.Services.SetAttachment(caseAttach);                    
                }
                BindAttachment();
                labelNoResults.Visible = false;
                repeaterResults.Visible = true;
                lnkAttachment.Visible = true;
            }
            else
            {
                labelNoResults.Visible = true;
                repeaterResults.Visible = false;
            }

        }

        //private void InitializeComponent()
        //{
        //    this.buttonUploadAttch.Click += new System.EventHandler(this.buttonUploadAttch_Click);
        //    this.Load += new System.EventHandler(this.Page_Load);

        //}

        protected void RadUpload1_FileExists(object sender, Telerik.Web.UI.Upload.UploadedFileEventArgs e)
        {
            int counter = 1;

            UploadedFile file = e.UploadedFile;

            string targetFolder = Server.MapPath(RadUpload1.TargetFolder);

            string targetFileName = Path.Combine(targetFolder,
                file.GetNameWithoutExtension() + counter.ToString() + file.GetExtension());

            while (System.IO.File.Exists(targetFileName))
            {
                counter++;
                targetFileName = Path.Combine(targetFolder,
                    file.GetNameWithoutExtension() + counter.ToString() + file.GetExtension());
            }

            file.SaveAs(targetFileName);
        }

        protected void lnkAttachment_Click(object sender, EventArgs e)
        {
            pnlClientFileUpload.Visible = true;
            (sender as LinkButton).Visible = false;           
        }

        protected void btnCancelUpl_Click(object sender, System.EventArgs e)
        {
            pnlClientFileUpload.Visible = false;
            lnkAttachment.Visible = true;       
        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            Services.CaseHeader cHeader = new Services.CaseHeader();
            cHeader.CaseID = caseHeader.CaseID;
            cHeader.CaseStatus = RadioButtonListStatus.SelectedItem.Value;
            Services.Services.UpdateCase(cHeader);            
        }
}
}